package ui.panels.distributionPanels;

import gameEngine.agents.choosingStrategy.representation.ManuallAdaptiveRepresentation;
import gameEngine.agents.choosingStrategy.representation.StrategyRepresentation;
import gameEngine.agents.distrebution.representation.DistributionRepresentation;
import gameEngine.agents.distrebution.representation.ExponentialRepresentation;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;

import org.apache.log4j.Logger;

import ui.flowManager.FlowManagerImpl;
import ui.flowManager.expirimentProperties.AgentRepresentation;
import ui.flowManager.expirimentProperties.ExperimentProperties.Agent_Group;
import ui.panels.commands.SetDistRepresentationCommand;
import ui.panels.commands.SetEnablecommand;
import Data.ConfigurationParameters.Distribution;
import auxiliary.AuxiliaryClass;

/**
 * @author vladimir && alon
 *	
 *	Panel for Exponential Distribution parameters
 */
public class ExponentialDistributionPanel extends JPanel{

	
	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	private JLabel 				jLabel1;
	private JLabel 				lambdaLabel;
	private JButton 			enter;
	private JTextField 			lambdaTextField;
	
	private DistributionFrame 	myFrame;
	private int 				gameNumber = 0;
	private int 				playerNumber = 0;
	Logger log = Logger.getLogger(this.getClass());
	
	/**
	 * CTOR
	 */
	public ExponentialDistributionPanel(DistributionFrame myFrame, int gameNumber, int plyerNumber, Agent_Group group)
	{
		super();
		//save
		this.myFrame = myFrame;
		this.gameNumber = gameNumber;
		this.playerNumber = plyerNumber;
		//initialize
		initPanel(group);
	}
	
	/**
	 * Initialize the Panel
	 */
	private void initPanel(Agent_Group group)
	{
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)this);
			this.setLayout(thisLayout);
			setPreferredSize(new Dimension(300, 250));
			{
				jLabel1 = new JLabel("Exponential Distribution", JLabel.CENTER);			
				jLabel1.setFont(new Font(Font.SERIF,Font.BOLD,18));

				lambdaLabel = new JLabel("lambda value", JLabel.CENTER);

				lambdaTextField = new JTextField("");

				enter = new JButton();
				enter.setText("Enter");
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addComponent(jLabel1, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
				.addGap(30)
				.addComponent(lambdaLabel, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
				.addComponent(lambdaTextField, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
				.addGap(24)
				.addComponent(enter, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
				.addContainerGap(81, Short.MAX_VALUE));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap(39, 39)
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(thisLayout.createSequentialGroup()
				        .addComponent(jLabel1, GroupLayout.PREFERRED_SIZE, 222, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 0, Short.MAX_VALUE))
				    .addGroup(thisLayout.createSequentialGroup()
				        .addGap(49)
				        .addGroup(thisLayout.createParallelGroup()
				            .addGroup(thisLayout.createSequentialGroup()
				                .addComponent(lambdaLabel, GroupLayout.PREFERRED_SIZE, 98, GroupLayout.PREFERRED_SIZE)
				                .addGap(0, 0, Short.MAX_VALUE))
				            .addGroup(thisLayout.createSequentialGroup()
				                .addGap(7)
				                .addGroup(thisLayout.createParallelGroup()
				                    .addGroup(thisLayout.createSequentialGroup()
				                        .addComponent(lambdaTextField, GroupLayout.PREFERRED_SIZE, 91, GroupLayout.PREFERRED_SIZE)
				                        .addGap(0, 0, Short.MAX_VALUE))
				                    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                        .addPreferredGap(lambdaTextField, enter, LayoutStyle.ComponentPlacement.INDENT)
				                        .addComponent(enter, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
				                        .addGap(0, 8, Short.MAX_VALUE)))))
				        .addGap(75)))
				.addContainerGap(39, 39));
		} catch (Exception e) {
			log.debug(e.toString());
		}
		
		//set the listener of enter button
		setListeners(group);
		
		//case player initialized  
		if(playerNumber > 0)
		{
			//set the GUI field
			setData(group);
		}
	}
	
	
	/**
	 * 
	 */
	private void setListeners(final Agent_Group group)
	{
		enter.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				String lambdaStr = lambdaTextField.getText();
				double lambda 	= 0;
				
				//parse the lambda 
				if(AuxiliaryClass.isDouble(lambdaStr))
					lambda = Double.parseDouble(lambdaStr);
				
				// create DistributionRepresentation and set it 
				ExponentialRepresentation expoRepresentation = new ExponentialRepresentation(myFrame.getDistribution(), lambda);			
				SetDistRepresentationCommand setDistRepresentationCommand = new SetDistRepresentationCommand();
				setDistRepresentationCommand.setDistRepresentation(expoRepresentation);
				setDistRepresentationCommand.setGroup(group);
				setDistRepresentationCommand.execute();
				
				//set enable the prime GUI , use via command pattern
				SetEnablecommand enableCommand = new SetEnablecommand();
				enableCommand.setArg(true);
				enableCommand.execute();
				
				//close the frame
				myFrame.dispose();
			}
		});
	}
	
	/**
	 * If already initialized set the data
	 */
	private void setData(Agent_Group group)
	{
		//first check if previous Distribution was also Exponential	
		AgentRepresentation agRepresentation = FlowManagerImpl.
				getInstance().getProps().getPlayer(gameNumber, playerNumber, group);
		
		DistributionRepresentation distRepresentation = agRepresentation.getDistributionType();
		
		//case not Exponential
		if(! (distRepresentation instanceof ExponentialRepresentation))
		{
			return;
		}
		
		//cast to Exponential
		ExponentialRepresentation exponentialRepresentation = (ExponentialRepresentation) distRepresentation;
		double lambda = exponentialRepresentation.getLambda();
		//set in the field
		lambdaTextField.setText(Double.toString(lambda));
	}

}
